package demo.xlj.yuyi.studentapp.shetuan;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.common.CommunityStyleActivity;
import demo.xlj.yuyi.studentapp.common.FuZhongStyleActivity;
import demo.xlj.yuyi.studentapp.common.LookAnnouncementActivity;
import demo.xlj.yuyi.studentapp.common.LookMyTongZhiActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationHuoDongActivity;
import demo.xlj.yuyi.studentapp.common.SendTongZhiChooseActivity;
import demo.xlj.yuyi.studentapp.common.SocialActivity;
import demo.xlj.yuyi.studentapp.common.StudentStyleActivity;
import demo.xlj.yuyi.studentapp.common.WhereSchoolActivity;
import demo.xlj.yuyi.studentapp.common.YouthLeagueNewsActivity;
import demo.xlj.yuyi.studentapp.login.StudentMeetLoginActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;

/**
 * 作者：项路俊 on 2018/4/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：社团身份的新界面
 */

public class SheTActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_She_Tuan)
    ImageView mImageViewSheTuan;
    @BindView(R.id.ImageSwitcher_she_tuan)
    ImageSwitcher mImageSwitcherSheTuan;
    @BindView(R.id.LinearLayout_She_Tuan_One)
    LinearLayout mLinearLayoutSheTuanOne;
    @BindView(R.id.LinearLayout_She_Tuan_Two)
    LinearLayout mLinearLayoutSheTuanTwo;
    @BindView(R.id.LinearLayout_She_Tuan_Three)
    LinearLayout mLinearLayoutSheTuanThree;
    @BindView(R.id.LinearLayout_She_Tuan_Four)
    LinearLayout mLinearLayoutSheTuanFour;
    @BindView(R.id.LinearLayout_She_Tuan_Five)
    LinearLayout mLinearLayoutSheTuanFive;
    @BindView(R.id.LinearLayout_She_Tuan_Six)
    LinearLayout mLinearLayoutSheTuanSix;
    @BindView(R.id.LinearLayout_She_Tuan_Seven)
    LinearLayout mLinearLayoutSheTuanSeven;
    @BindView(R.id.LinearLayout_She_Tuan_Eight)
    LinearLayout mLinearLayoutSheTuanEight;
    @BindView(R.id.LinearLayout_She_Tuan_Nine)
    LinearLayout mLinearLayoutSheTuanNine;
    @BindView(R.id.LinearLayout_She_Tuan_Ten)
    LinearLayout mLinearLayoutSheTuanTen;
    @BindView(R.id.LinearLayout_She_Tuan_Eleven)
    LinearLayout mLinearLayoutSheTuanEleven;
    @BindView(R.id.LinearLayout_She_Tuan_Twen)
    LinearLayout mLinearLayoutSheTuanTwen;
    @BindView(R.id.Drawer_Layout_She_Tuan)
    DrawerLayout mDrawerLayoutSheTuan;
    @BindView(R.id.NavigationView_She_Tuan)
    NavigationView mNavigationViewSheTuan;
    private int[] images = {R.drawable.lunbo4, R.drawable.lunbo5, R.drawable.lunbo6, R.drawable.lunbo7};//图片资源数组
    private int position = 0;

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    position++;
                    mImageSwitcherSheTuan.setImageResource(images[position]);
                    if (position == 3) {
                        position = -1;
                    }
                    myHandler.sendEmptyMessageDelayed(11, 4000);

                    break;
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.s_t_activity);
        ButterKnife.bind(this);
        String name = (String) SharePerferencesUtils.get(SheTActivity.this, "name", "", 0);

        ToastUtil.success(SheTActivity.this, "欢迎" + name + "归来！", true, true);
        initImageView();
        View view = mNavigationViewSheTuan.getHeaderView(0);

        TextView textView_Name = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Name);
        TextView textView_ShenFen = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Shen_Fen);

        textView_Name.setText("用户姓名"+name);
        textView_ShenFen.setText("用户身份：社团");
        initListener();
    }

    private void initListener() {
        if (mNavigationViewSheTuan != null) {
            mNavigationViewSheTuan.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();

                    if (id == R.id.nav_camera) {
                        //表示退出登录
                        ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, StudentMeetLoginActivity.class);
                        SheTActivity.this.finish();
                    } else if (id == R.id.nav_gallery) {
                        //表示我收藏的社团
                        ActivityUtil.openActivityFromLeftToRightByResult(SheTActivity.this, CommunityStyleActivity.class, "type", "1");
                    } else if (id == R.id.nav_slideshow) {
                        //表示我的发布
                        ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, LookMyTongZhiActivity.class);
                    }else if (id == R.id.nav_myhave){
                        ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, MyCommicationHuoDongActivity.class);
                    }

                    return true;
                }
            });
        }

        if (mImageViewSheTuan != null) {
            mImageViewSheTuan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawerLayoutSheTuan.openDrawer(GravityCompat.START);
                }
            });
        }
    }


    //初始化，上面的轮播图
    private void initImageView() {
        mImageSwitcherSheTuan.setFactory(new ViewSwitcher.ViewFactory() {//设定工厂，每进来一个图片都用一个ImageView接收
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(SheTActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });//这个设置Factory是必须的
        mImageSwitcherSheTuan.setImageResource(images[position]);//设置初始图片,也是通过这个代码设置在控件上显示的图片
        mImageSwitcherSheTuan.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//动画淡入
        mImageSwitcherSheTuan.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//动画淡出

        myHandler.sendEmptyMessageDelayed(11, 3000);

        mImageSwitcherSheTuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击事件
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, WhereSchoolActivity.class);
            }
        });
    }


    @OnClick({ R.id.LinearLayout_She_Tuan_One, R.id.LinearLayout_She_Tuan_Two, R.id.LinearLayout_She_Tuan_Three, R.id.LinearLayout_She_Tuan_Four, R.id.LinearLayout_She_Tuan_Five, R.id.LinearLayout_She_Tuan_Six, R.id.LinearLayout_She_Tuan_Seven, R.id.LinearLayout_She_Tuan_Eight, R.id.LinearLayout_She_Tuan_Nine, R.id.LinearLayout_She_Tuan_Ten, R.id.LinearLayout_She_Tuan_Eleven, R.id.LinearLayout_She_Tuan_Twen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.LinearLayout_She_Tuan_One:
                //附中风采
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, FuZhongStyleActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Two:
                //社团风貌
                ActivityUtil.openActivityFromLeftToRightByResult(SheTActivity.this, CommunityStyleActivity.class,"type","0");
                break;
            case R.id.LinearLayout_She_Tuan_Three:
                //附中要闻
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, YouthLeagueNewsActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Four:
                //社团活动
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, SocialActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Five:
                //学生会风采
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, StudentStyleActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Six:
                //公告通知
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, LookAnnouncementActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Seven:
                //发布通知
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, SendTongZhiChooseActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Eight:
                //申请活动
                String ke [] = {"content","who","address","time"};
                String va [] = {"","","","请点击选择时间"};
                ActivityUtil.openActivityFromLeftToRightByMoreResuslt(SheTActivity.this, ApplyHuoDongActivity.class,ke,va);
                break;
            case R.id.LinearLayout_She_Tuan_Nine:
                //我的
                ActivityUtil.openActivityForResultFromLeftToRight(SheTActivity.this, MyCommicationActivity.class, 99);
                break;
            case R.id.LinearLayout_She_Tuan_Ten:
                ToastUtil.info(SheTActivity.this,"暂时没有更多",true,true);
                break;
            case R.id.LinearLayout_She_Tuan_Eleven:
                //附中要闻
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, YouthLeagueNewsActivity.class);
                break;
            case R.id.LinearLayout_She_Tuan_Twen:
                //社团活动
                ActivityUtil.openActivityFromLeftToRight(SheTActivity.this, SocialActivity.class);
                break;
        }
    }
}
